//
//  ReadListInfoViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 张凡. All rights reserved.
//

#import "ReadListInfoViewController.h"

@interface ReadListInfoViewController ()<UIWebViewDelegate>

@property (nonatomic, strong)UIWebView *webView;

@end

@implementation ReadListInfoViewController

- (UIWebView *)webView{
    
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:self.view.frame];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
        _webView.scrollView.bouncesZoom = NO;
    }
    return _webView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.webView];
    
    //请求数据
    [RequsetManager fetchDataWithUrl:kReadListInfoUrl parametr:@{@"contentid":self.contentID} mehtod:@"POST" complete:^(NSData *data) {
        
        NSDictionary *bigDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@", bigDic[@"data"][@"html"]);
        //根据<pre>进行切割
        NSArray *array = [bigDic[@"data"][@"html"] componentsSeparatedByString:@"<pre>"];
        [self.webView loadHTMLString:array[0] baseURL:nil];
    } updateUI:^{
        
    }];
    
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
}

#pragma mark ------UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];

    NSLog(@"加载完成");
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    NSLog(@"开始加载");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
